<template>
	<view>
		<!--不要公共模块-->
		<!--isFullScreen 可以实其全屏显示-->
		<view style="text-align: center">
			<image src="/static/logo.png" mode="aspectFit" style="width: 260rpx; height: 140rpx"></image>
		</view>
		<view class="account-form-box">
			<uni-forms :model="model" ref="uForm" :errorType="form.errorType">
				<uni-forms-item class="form-item"  prop="username" >
					<text style="font-weight:700;">用户名</text>
					<view style="height: 20rpx;"></view>
					<input v-model="model.username" placeholder="请填写用户名" type="text" />
				</uni-forms-item>
				<uni-forms-item class="form-item"  prop="password" >
					<text style="font-weight:700;">密码</text>
					<view style="height: 20rpx;"></view>
					<input v-model="model.password" placeholder="请填写密码" type="password" />
				</uni-forms-item>
			</uni-forms>

			<view style="height: 40rpx;"></view>

			<button class="button" type="primary" @click="weixin">登录</button>
			<view style="height: 40rpx;"></view>
			<!-- 			<view class="u-flex u-p-l-20 u-p-r-20">
				<view class="u-flex-1 u-text-left" @click="">验证码登陆</view>
				<view class="u-flex-1 u-text-center" @click="weixin">微信登录</view>
				<view class="u-flex-1 u-text-right" @click="">忘记密码</view>
			</view> -->
			<view class="u-flex-1 u-text-center" @click="register">注册</view>
		</view>

		<view style="height: 40rpx;"></view>
	</view>
</template>

<script>
	export default {

		data() {
			return {
				loadstxt: 'loading',
				form: {
					errorType: ["message"],
					labelPosition: "top",
				},
				model: {
					/* 账号 */
					username: "",
					/* 密码 */
					password: "",
				},
				rules: {
					username: [{
						required: true,
						message: "请输入用户名",
						trigger: ["change", "blur"],
					}, ],
					password: [{
							required: true,
							message: "请输入密码",
							trigger: ["change", "blur"],
						},
						{
							min: 6,
							max: 20,
							message: '密码长度在6到20个字符',
							trigger: ["change", "blur"],
						},
						{
							pattern: /^[0-9a-zA-Z]*$/g,
							transform(value) {
								return String(value);
							},
							message: '密码为字母或数字',
							trigger: ["change", "blur"],
						},
					],
				},
			};
		},

		methods: {
			submit() {
				this.$refs.uForm.validate((valid) => {
					if (valid) {
						uni.request({
							url: "http://localhost:8081/user/login",
							data: {
								type: "User",
								uphone: this.model.username,
								upassword: this.model.password,
							},
							method: "POST"
						}).then(res => {
							console.log(res[1].data);
							if (res[1].data.code == 200) {
								uni.showToast({
									title: "登录成功",
									icon: "success",
									duration: 1000
								});
								getApp().globalData.username = res[1].data.data.uname;
								getApp().globalData.password = res[1].data.data.upassword;
								getApp().globalData.job = res[1].data.data.ujob;
								getApp().globalData.phone = res[1].data.data.uphone;
								getApp().globalData.level = res[1].data.data.ulevel;
								getApp().globalData.id = res[1].data.data.uid;
								console.log(getApp().globalData);
								// uni.navigateTo({
								// 	url: "/pages/normal/main"
								// })

								if (res[1].data.data.ulevel == 1) {
									setTimeout(function() {
										uni.redirectTo({
											url: "./main"
										})
									}, 1000);
								} else if (res[1].data.data.ulevel == 2)
									setTimeout(function() {
										uni.redirectTo({
											url: 'main'
										})
									}, 1000);
								else if (res[1].data.data.ulevel == 3)
									setTimeout(function() {
										uni.redirectTo({
											url: "../main/main"
										})
									}, 1000);;
							} else if (res[1].data.code == 500) {
								uni.showToast({
									icon: "error",
									title: "系统出错"
								})
								return;
							} else if (res[1].data.code == 400 || res[1].data.code == 401) {
								console.log("???");
								uni.showToast({
									icon: "error",
									title: "账号密码错误"
								})
							}
						})
					}
				});


			},
			register() {
				uni.navigateTo({
					url: "register"
				})
			},
			weixin() {
				wx.login({
					desc: '用于完善用户资料',
					success: (res) => {
						console.log("获取成功", res);
						uni.request({
							url: "http://localhost:8081/user/loginByWX?code=" + res.code,
							method: "GET",
						}).then(result => {
							console.log(result[1].data.data);
							if (result[1].data.code == 200) {
								uni.getUserProfile({
									desc: "用于完善用户信息",
									success: (res1) => {
										getApp().globalData.userWechatInfo = res1.userInfo;
										console.log(res1)
										uni.showToast({
											icon: "none",
											title: '获取成功'
										})
									},
									fail: (err) => {
										console.log(err)
										uni.showToast({
											icon: "none",
											title: '用户拒绝获取'
										})
								 }
								});
								uni.showToast({
									title: "登录成功",
									icon: "success",
									duration: 1000
								});
								getApp().globalData.username = result[1].data.data.uname;
								getApp().globalData.password = result[1].data.data.upassword;
								getApp().globalData.job = result[1].data.data.ujob;
								getApp().globalData.phone = result[1].data.data.uphone;
								getApp().globalData.level = result[1].data.data.ulevel;
								getApp().globalData.id = result[1].data.data.uid;
								if (result[1].data.data.ulevel == 1)
									setTimeout(function() {
										uni.redirectTo({
											url: "../my/my"
										})
									}, 1000);

								else if (result[1].data.data.ulevel == 2)
									setTimeout(function() {
										uni.navigateTo({
											url: 'main'
										})
									}, 1000);
								else if (result[1].data.data.ulevel == 3)
								;
							} else if (result[1].data.code == 500) {
								uni.showToast({
									icon: "error",
									title: "系统出错"
								})
								return;
							} else if (result[1].data.code == 400) {
								console.log("???");
								uni.showToast({
									icon: "error",
									title: "用户未绑定微信"
								})
							}
						})
					},
					fail: (err) => {
						uni.showToast({
							title: "获取失败",
							icon: "error"
						})
					}
				});
			},
		},

		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
	};
</script>

<style lang="scss" scoped>
	.account-form-box {
		padding: 0 32rpx;
	}
	.button{
		width: 90%;
		height: 80rpx;
		background-color: #55aa7f;
	}
</style>
